package com.wingstudio.springcloud.service;

//import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.IdUtil;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.wingstudio.springcloud.redis.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @Author ITcz
 * @Data 2021-04-15 - 16:30
 */
@Service
@Slf4j
public class PaymentService {

    @Autowired
    RedisService redisService;

    @GetMapping("/lock/{id}")
    public String paymentInfo_Lock(@PathVariable("id") Integer id) {
//        String result = paymentService.paymentInfo_OK(id);
        String requestId = UUID.randomUUID().toString();
        String lockId = id.toString();
        boolean result = redisService.lock("paymentInfo", lockId, requestId);
        log.info("**********result" +result);
        return result + "";
    }

    /**
    * 正常访问
    * */
    public String paymentInfo_OK(Integer id) {
        return "线程池: " + Thread.currentThread().getName()+" paymentInfo_OK, id: " + id;
    }

    /**
     * 异常访问
     * */
    @HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler",
            commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")})
    public String paymentInfo_ERROR(Integer id) {
        int time = 3;
        try{
            TimeUnit.SECONDS.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "线程池: " + Thread.currentThread().getName()+" paymentInfo_OK, id: " + id +'\t'+"耗时: " + time + "秒";
    }

    protected String paymentInfo_TimeOutHandler(Integer id){
        return "线程池：" + Thread.currentThread().getName() + "业务繁忙，请稍后再试." ;
    }


    //以下为服务熔断=================
    @HystrixCommand(fallbackMethod = "paymentCircuitBreaker_fallback", commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),//请求次数
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"),//时间窗口期
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60"),//失败率达到多少后跳闸
    })
    public String paymentCircuitBreaker(@PathVariable("id") Integer id){
        if (id < 0) {
            throw  new RuntimeException("*******id不能为负!");
        }
        String serialNumber = IdUtil.simpleUUID();
        return Thread.currentThread().getName()+"\t调用成功，流水号为：" + serialNumber;
    }
    public String paymentCircuitBreaker_fallback(@PathVariable("id") Integer id) {
        return "id 不能为负，请稍后再试。id = " + id;
    }
}
